package cn.miaopasi.boot.clickhouse.service.impl;

import cn.miaopasi.boot.clickhouse.mapper.LoginLogMapper;
import cn.miaopasi.boot.clickhouse.model.LoginLog;
import cn.miaopasi.boot.clickhouse.model.dto.LoginLogDTO;
import cn.miaopasi.boot.clickhouse.service.ILoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 登录日志接口实现
 *
 * @author Administrator
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {
    @Override
    public Page<LoginLog> page(LoginLogDTO loginLog) {
        // 分页查询
        Page<LoginLog> pageObj = new Page<>();
        pageObj.setCurrent(loginLog.getCurrent());
        pageObj.setSize(loginLog.getSize());
        pageObj.addOrder(OrderItem.desc(LoginLog.LOGIN_DATE_TIME));
        // 组装查询条件
        LambdaQueryWrapper<LoginLog> queryWrapper = new LambdaQueryWrapper<>();
        // appId
        if (null != loginLog.getAppId() && !"".equals(loginLog.getAppId())) {
            queryWrapper.eq(LoginLog::getAppId, loginLog.getAppId());
        }
        // 查询时间段
        if (null != loginLog.getStartDateTime() && null != loginLog.getEndDateTime()) {
            queryWrapper.between(LoginLog::getLoginDateTime, loginLog.getStartDateTime(), loginLog.getEndDateTime());
        }
        // IP
        if (null != loginLog.getIp()) {
            queryWrapper.eq(LoginLog::getIp, loginLog.getIp());
        }
        // username
        if (null != loginLog.getUsername() && !"".equals(loginLog.getUsername())) {
            queryWrapper.eq(LoginLog::getUsername, loginLog.getUsername());
        }
        // isSuccess
        if (null != loginLog.getIsSuccess()) {
            queryWrapper.eq(LoginLog::getIsSuccess, loginLog.getIsSuccess());
        }
        return page(pageObj, queryWrapper);
    }
}
